package com.chunuo.eduservice.controller;


import com.chunuo.commonutils.JwtUtils;
import com.chunuo.commonutils.R;
import com.chunuo.commonutils.ordervo.UcenterMemberOrder;
import com.chunuo.eduservice.controller.front.CourseFrontController;
import com.chunuo.eduservice.entity.EduComment;
import com.chunuo.eduservice.entity.frontvo.CourseWebVo;
import com.chunuo.eduservice.openfeign.UcenterMemberOpenFeign;
import com.chunuo.eduservice.service.EduCommentService;
import com.chunuo.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    @Autowired
    private UcenterMemberOpenFeign ucenterMemberOpenFeign;

    @Autowired
    private EduCourseService eduCourseService;
    /**
     * 获取课程的评论
     * @param courseId
     * @return
     */
    @GetMapping("getCourseComment/{courseId}")
    public R getCourseComment(@PathVariable("courseId")String courseId){
        List<EduComment> commentList =  eduCommentService.getCourseComment(courseId);
        return R.ok().data("commentList",commentList);
    }

    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request){
        System.out.println("comment = "+comment);
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(comment.getCourseId());
        //获取用户的信息
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberOrder userInfo = ucenterMemberOpenFeign.getInfo(userId);
        //设置用户相关信息
        comment.setMemberId(userInfo.getId());
        comment.setNickname(userInfo.getNickname());
        comment.setAvatar(userInfo.getAvatar());
        //设置课程相关信息
        comment.setCourseId(courseWebVo.getId());
        comment.setTeacherId(courseWebVo.getTeacherId());

        eduCommentService.save(comment);

        return R.ok();
    }

}

